package com.macro.mall.seckill.service.impl;

import com.macro.mall.model.SmsFlashPromotion;
import com.macro.mall.seckill.service.SecKillService;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

/**
 * @program: mall
 * @ClassName: SecKillServiceImpl
 * @description: 秒杀业务相关实现
 * @author: tan
 * @create: 2024-06-25 15:58
 **/
@Service
public class SecKillServiceImpl implements SecKillService {

    @Override
    public SmsFlashPromotion findCurrentPromotion(List<SmsFlashPromotion> promotions) {
        SmsFlashPromotion currentPromotion = null;
        long diffTime = Long.MAX_VALUE;
        for (SmsFlashPromotion smsFlashPromotion:promotions){
            Date startDate = smsFlashPromotion.getStartDate();
            long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
            long startTime = startDate.getTime();
            if(now-startTime<diffTime){
                currentPromotion = smsFlashPromotion;
            }
        }
        return currentPromotion;
    }
}
